package com.lei.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lei.servicebase.exception.GuliException;
import com.lei.utils.Result;
import com.lei.vod.service.VodService;
import com.lei.vod.utils.ConstantUtil;
import com.lei.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
@RestController
@RequestMapping("/edu/vod")
public class VodController {
    @Autowired
    private VodService vodService;
    //上传视频到阿里云视频点播
    @PostMapping("/upload")
    public Result uploadVideo(MultipartFile file){
        String videoId=vodService.uploadVideo(file);
        return Result.ok().data("videoId",videoId);
    }
    //删除阿里云视频
    @DeleteMapping("/deleteVideo/{videoId}")
    public Result deleteVideo(@PathVariable("videoId") String videoId){
        try {
            DefaultAcsClient client = InitVodClient.getAcsClient(ConstantUtil.ACCESS_KEY_ID, ConstantUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
        return Result.ok();
    }

    //删除多个阿里云视频,删除课程时使用
    @DeleteMapping("/deleteBatch")
    public Result deleteVideoBatchIds(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeAllVideo(videoIdList);

        return Result.ok();
    }

    //根据视频Id获取,删除视频播放凭证
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable("id")String id){
        try {
            DefaultAcsClient acsClient = InitVodClient.getAcsClient(ConstantUtil.ACCESS_KEY_ID, ConstantUtil.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = acsClient.getAcsResponse(request);
            return Result.ok().data("playAuth",response.getPlayAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取凭证失败");
        }


    }

}
